package com.example.zhu95.mytimber.adapters;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.listener.AlbumClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class AlbumGridAdapter extends RecyclerView.Adapter<AlbumGridAdapter.ViewHolder> {
    private List<AlbumBean> albums;
    private AlbumClickListener albumClickListener;

    public void setAlbums(List<AlbumBean> albums) {
        if (albums == this.albums) {
            this.albums.clear();
            this.albums.addAll(albums);
        }
    }

    public AlbumGridAdapter(List<AlbumBean> albums, AlbumClickListener albumClickListener) {
        this.albums = albums;
        this.albumClickListener = albumClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item_grid, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final AlbumBean album = albums.get(position);
        holder.gridItemTv1.setText(album.getAlbum());
        holder.gridItemTv2.setText(album.getArtist());
        String albumArt = album.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).centerCrop().into(holder.gridItemImg);
            //MARK 使用Palette
            // http://www.jianshu.com/p/4dc897dc5354
            // http://blog.csdn.net/yxw_android/article/details/51768414  有Glide的配合Palette的
            Bitmap bitmap = BitmapFactory.decodeFile(albumArt);
            if (bitmap != null) {
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        Palette.Swatch swatch = palette.getVibrantSwatch();
                        if (swatch == null) {
                            swatch = palette.getMutedSwatch();
                        }
                        if (swatch != null) {
                            // defect: 2017/12/10 然而字体颜色与人家的颜色反了
                            int bodyTextColor = swatch.getBodyTextColor();
                            holder.gridItemTv1.setTextColor(bodyTextColor);
                            holder.gridItemTv2.setTextColor(bodyTextColor);
                            holder.gridItemBotttom.setBackgroundColor(swatch.getRgb());
                        }
                    }
                });
            }
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.gridItemImg);
            holder.gridItemBotttom.setBackgroundColor(Color.WHITE);
            holder.gridItemTv1.setTextColor(Color.BLACK);
            holder.gridItemTv2.setTextColor(Color.BLACK);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                albumClickListener.clickAlbum(album.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return albums.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.grid_item_img)
        ImageView gridItemImg;
        @BindView(R.id.grid_item_tv1)
        TextView gridItemTv1;
        @BindView(R.id.grid_item_tv2)
        TextView gridItemTv2;
        @BindView(R.id.grid_item_botttom)
        RelativeLayout gridItemBotttom;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
